package org.weasel.controllers;


import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.weasel.models.ChangeListener;
import org.weasel.tree.TreeObjectIteration;
import org.weasel.views.IterationChartComposite;
import org.weasel.views.IterationDetail;

public class IterationViewController implements ChangeListener {
	private static IterationViewController instance = null;
	private ApplicationController appController;
	private TreeObjectIteration to_iteration;
	private IterationDetail iterationView;
	private IterationChartController iterationChartController;
	
	public static IterationViewController instance() {
		if(instance == null)
			instance = new IterationViewController();
		return instance;
	}
	
	private IterationViewController() {
	}
	
	public void setApplicationController(ApplicationController appController){
		this.appController = appController;
		this.appController.registerListener(this);
	}

	public void setTo_iteration(TreeObjectIteration to_iteration) {
		this.to_iteration = to_iteration;
	}

	public void showIterationView() {
		if(iterationChartController==null){
			iterationChartController = new IterationChartController();
			iterationChartController.setProjectViewController(this);
		}
		iterationChartController.loadUserStories(to_iteration.getIteration().getUserStories());
		try {
//			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IterationDetail.ID, to_iteration.getIteration().getId(),IWorkbenchPage.VIEW_ACTIVATE);
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IterationDetail.ID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
//		IViewReference ref =  page.findViewReference(IterationDetail.ID,to_iteration.getIteration().getId());
//		IViewPart viewPart = ref.getView(false);
//		iterationView = (IterationDetail) viewPart;
		iterationView = (IterationDetail) page.findView(IterationDetail.ID);
		iterationView.showIterationInformation(to_iteration.getIteration());
		iterationChartController.refreshComposite();
	}

	public void setNotes(String text) {
		to_iteration.getIteration().setNotes(text);
		appController.changesOccured();
	}

	public void setIterationName(String text) {
		to_iteration.getIteration().setName(text);
		to_iteration.setName(text);
		appController.refreshTreeViewer();
		appController.changesOccured();
	}
	
	public IterationChartComposite createIterationChartComposite(Composite parent, int style){
		return iterationChartController.loadIterationChartComposite(parent, style);
	}

	public void updateIterationViews() {
		if(iterationView!=null){
			iterationView.showIterationInformation(to_iteration.getIteration());
			iterationChartController.refreshComposite();
		}
	}

	public void changeHappened() {
		if(iterationChartController!=null)
			iterationChartController.refreshComposite();
	}

	public void doViewClosedActions() {
		appController.unregisterListener(this);		
	}

}
